package com.lh.job.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lh.common.entity.admin.DoboCode;
import com.lh.common.entity.admin.Order;
import com.lh.common.entity.admin.Period;
import com.lh.common.entity.admin.UserCode;
import com.lh.common.enums.OrderStatusEnum;
import com.lh.common.enums.OrderTypeEnum;
import com.lh.common.enums.TrueFalseEnum;
import com.lh.job.dao.PeriodMapper;
import com.lh.job.service.PeriodService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author FH
 * @ClassName: PeriodService
 * @Description: (类作用)
 * @date: 2020-05-21 17:08
 */
@Service
public class PeriodServiceImpl extends ServiceImpl<PeriodMapper, Period> implements PeriodService {

    @Override
    public int insertPeriod(Period period) {
        return this.baseMapper.insertPeriod(period);
    }

    @Override
    public List<Period> queryPeriodList() {
        return this.baseMapper.queryPeriodList();
    }

    @Override
    public Period queryPeriod(Long aid) {
        return this.baseMapper.queryPeriod(aid);
    }

    @Override
    public BigDecimal sumOrderPrice(Long pid) {
        return this.baseMapper.sumOrderPrice(pid);
    }

    @Override
    public UserCode queryRobotCodeList(int portionNum,Long pid,Long aid,BigDecimal shopActivityPrice) {
        UserCode userCode = this.baseMapper.queryRobotCodeList(portionNum);
        if(userCode == null){
            userCode = new UserCode();
            userCode.setPeriodCode("99999999");
            userCode.setOrderItem("MK--------------");
            userCode.setName("虚拟");
            userCode.setUid(0l);
        }
        Order order = new Order();
        String orderItem = generateCode();
        userCode.setOrderItem(orderItem);
        order.setOrderItem(orderItem);
        order.setShouldPrice(shopActivityPrice);
        order.setOrderType(OrderTypeEnum.ONEDOLLARRUSH);
        order.setOrderStatus(OrderStatusEnum.COMPLETED);
        order.setUserId(userCode.getUid());
        order.setAid(aid);
        order.setPeriodId(pid);
        this.baseMapper.insertOrder(order);
        DoboCode doboCode = new DoboCode();
        doboCode.setPeriodId(pid);
        doboCode.setUserId(userCode.getUid());
        doboCode.setOrderItem(orderItem);
        doboCode.setPeriodCode(userCode.getPeriodCode());
        doboCode.setResult(TrueFalseEnum.TRUE);
        doboCode.setBuyTime(new Date());
        this.baseMapper.insertDoboCode(doboCode);
        return userCode;
    }

    @Override
    public String queryCode(Long pid, int portionNum) {
        return this.baseMapper.queryCode(pid,portionNum);
    }

    @Override
    public UserCode queryUserCodeList(String code, Long pid) {
        return this.baseMapper.queryUserCodeList(code,pid);
    }

    @Override
    public int updatePeriod(Period period) {
        return this.baseMapper.updatePeriod(period);
    }

    @Override
    public int updatePeriodRobotBuy(Period period) {
        return this.baseMapper.updatePeriodRobotBuy(period);
    }

    //生成订单号
    private static final Lock LOCK = new ReentrantLock();
    private static long BEGIN_INDEX = 10000;
    private static final long END_INDEX = 19999;
    final private String fixedCode = "MHW";
    public String generateCode(){
        LOCK.lock();
        String code = "";
        try {
            SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMddHHmmssSSS");
            Date current = new Date();
            String date = sdf.format(current);
            if(BEGIN_INDEX >= END_INDEX ){
                BEGIN_INDEX = 10001;
            }else {
                BEGIN_INDEX = BEGIN_INDEX + 1;
            }
            String str = String.valueOf(BEGIN_INDEX);
            code = fixedCode + (date +str.substring(1, str.length()));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            LOCK.unlock();
        }
        return code;
    }
}
